package com.ruoyi.depot.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.ProductCates;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.depot.Monetary;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.depot.mapper.MonetaryMapper;
import com.ruoyi.depot.service.IMonetaryService;

/**
 * 货币单位Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-02
 */
@Service
public class MonetaryServiceImpl implements IMonetaryService 
{
    @Autowired
    private MonetaryMapper monetaryMapper;

    /**
     * 查询货币单位
     * 
     * @param monetaryId 货币单位主键
     * @return 货币单位
     */
    @Override
    public Monetary selectMonetaryByMonetaryId(Long monetaryId)
    {
        return monetaryMapper.selectMonetaryByMonetaryId(monetaryId);
    }

    /**
     * 查询货币单位列表
     * 
     * @param monetary 货币单位
     * @return 货币单位
     */
    @Override
    @DataScope(MonetaryAlias = "m")
    public List<Monetary> selectMonetaryList(Monetary monetary)
    {
        return monetaryMapper.selectMonetaryList(monetary);
    }

    /**
     * 新增货币单位
     * 
     * @param monetary 货币单位
     * @return 结果
     */
    @Override
    public int insertMonetary(Monetary monetary)
    {
        monetary.setAddTime(DateUtils.getNowDate());
        return monetaryMapper.insertMonetary(monetary);
    }

    /**
     * 修改货币单位
     * 
     * @param monetary 货币单位
     * @return 结果
     */
    @Override
    public int updateMonetary(Monetary monetary)
    {
        monetary.setUpdateTime(DateUtils.getNowDate());
        return monetaryMapper.updateMonetary(monetary);
    }

    /**
     * 批量删除货币单位
     * 
     * @param monetaryIds 需要删除的货币单位主键
     * @return 结果
     */
    @Override
    public int deleteMonetaryByMonetaryIds(Long[] monetaryIds)
    {
        return monetaryMapper.deleteMonetaryByMonetaryIds(monetaryIds);
    }

    /**
     * 删除货币单位信息
     * 
     * @param monetaryId 货币单位主键
     * @return 结果
     */
    @Override
    public int deleteMonetaryByMonetaryId(Long monetaryId)
    {
        return monetaryMapper.deleteMonetaryByMonetaryId(monetaryId);
    }

    @Override
    public List<TreeSelect> selectMonetaryTreeList(Monetary monetary) {
        List<Monetary> monetaryList = SpringUtils.getAopProxy(this).selectMonetaryList(monetary);
        return buildMonetaryTreeSelect(monetaryList);
    }

    @Override
    public List<Monetary> buildMonetaryTree(List<Monetary> monetaryList) {
        List<Monetary> returnList = new ArrayList<>();
        List<Long> collect = monetaryList.stream().map(Monetary::getMonetaryId).collect(Collectors.toList());
        for (Monetary monetary : monetaryList){
            //如果是顶节点，遍历该父节点下的所以子节的
            if(!collect.contains(monetary.getParentId())){
                recursionFn(monetaryList,monetary);
                returnList.add(monetary);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = monetaryList;
        }
        return returnList;
    }

    @Override
    public List<TreeSelect> buildMonetaryTreeSelect(List<Monetary> monetaryList) {
        List<Monetary> monetaryTrees = buildMonetaryTree(monetaryList);
        return monetaryTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     * @param monetaryList 子级
     * @param monetary 父级
     */
    private void recursionFn(List<Monetary> monetaryList , Monetary monetary){
        // 得到子节点列表
        List<Monetary> childList = getChildList(monetaryList, monetary);
        monetary.setChildrenMonetary(childList);
        for (Monetary newMonetary : childList)
        {
            if (hasChild(monetaryList, newMonetary))
            {
                recursionFn(monetaryList, newMonetary);
            }
        }
    }

    /**
     * 获得子节点
     * @param monetaryList
     * @param monetary
     * @return
     */
    private List<Monetary> getChildList(List<Monetary> monetaryList, Monetary monetary){
        List<Monetary> returnList = new ArrayList<>();
        Iterator<Monetary> iterator = monetaryList.iterator();
        while (iterator.hasNext()){
            Monetary n = (Monetary) iterator.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == monetary.getMonetaryId().longValue())
            {
                returnList.add(n);
            }
        }
        return returnList;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Monetary> monetaryList, Monetary monetary)
    {
        return getChildList(monetaryList, monetary).size() > 0;
    }
}
